<template>
  <a-modal
    :visible="visible"
    width="25%"
    title="新建学院"
    @cancel="handleCancel"
    @ok="handleConfirm"
  >
    <div style="padding: 20px">
      <a-form :model="addForm" ref="formRef" :rules="rules">
        <a-form-item label="学院名称" name="xymc">
          <a-input v-model:value="addForm.xymc" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="学院代码" name="xydm">
          <a-input v-model:value="addForm.xydm" placeholder="请输入" />
        </a-form-item>
        <!-- <a-form-item label="负责教师" name="fzls">
          <a-select
            allowClear
            v-model:value="addForm.fzls"
            show-search
            :fieldNames="{ label: 'jsxm', value: 'account' }"
            placeholder="请选择教师（账号）"
            :options="teacherOptions"
          />
        </a-form-item> -->
      </a-form>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
  import { 
    onMounted, 
    ref, 
    // toRefs,
    // computed 
  } from 'vue';
  import { saveOrUpdateCollege,getXyAccountArr } from '/@/api/organization/collegeManage';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  // import { useUserStore } from '/@/store/modules/user';
  // const userStore = useUserStore();
  // const userInfo = <any>computed(() => userStore.getUserInfo);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    addForm: {
      type: Object,
      default: () => {},
    },
  });

  const rules = {
    xymc: [{ required: true, message: '学院名称不能为空', trigger: 'blur' }],
    xydm: [{ required: true, message: '学院代码不能为空', trigger: 'blur' }],
    fzls: [{ required: true, message: '负责教师不能为空', trigger: 'blur' }],
  };
  const formRef = ref();
  const emits = defineEmits(['refresh', 'cancel']);

  // const teacherOptions = ref([]);

  // // 教师列表
  // const getTeacherOptions = async () => {
  //   const { data } = await getXyAccountArr({
  //     pageSize: 0,
  //     distributeFlag:true
  //   });
  //   teacherOptions.value = data.filter((item)=>item.jsxm=`${item.jsxm}(${item.account})`);
  // };
  // TODO:调试 创建学院
  const handleConfirm = async () => {
    try {
      await formRef.value.validate();
      const { code, message } = await saveOrUpdateCollege(props.addForm);
      if (code === 200) {
        createMessage.success('操作成功');
        handleCancel();
        emits('refresh');
      } else {
        createMessage.error(message);
      }
    } catch (error) {
      createMessage.error('操作错误');
    }
  };
  const handleCancel = () => {
    formRef.value.resetFields();
    emits('cancel');
  };

  onMounted(() => {
    // getTeacherOptions();
  });
</script>

<style scoped lang="scss"></style>
